package com.example.demo.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.base.mapper.BaseService;
import com.example.demo.entity.BookComment;
import com.example.demo.entity.BookCore;
import com.example.demo.util.StringUtil;

/**
 * 相关书籍的信息的接口
 * 
 * @author wang
 *
 */
@Service
public class BookCoreServiceImpl extends BaseService<BookCore> {

	@Autowired
	private BookAssistServiceImpl bookAssistServiceImpl;
	@Autowired
	private BookCommentServiceImpl bookCommentServiceImpl;
	/**
	 * 处理各个分段的百分比信息
	 */
	public void bookPercent(BookCore bookCore, List<BookComment> bookCommentList, Map<String, Object> paramMap) {
		String numberList[] = new String[5];
		// 评论总数
		int totalNumber = bookCommentList.size();
		for (Integer i = 0; i < numberList.length; i++) {

			paramMap.put("gradeStart", i * 2);
			if (i == numberList.length - 1) {
				paramMap.put("gradeEnd", i * 2 + 2 + 1);
			} else {
				paramMap.put("gradeEnd", i * 2 + 2);
			}
			// 计算各个分段的人数
			Long sectionNumber = bookCommentServiceImpl.getByMapTotal(paramMap);
			if (StringUtil.isEmpty(sectionNumber)) {
				numberList[i] = "0.00";
			} else {
				Float percentNumber = sectionNumber * 100f / totalNumber;
				String str[] = percentNumber.toString().split("[.]");
				if (str[1].length() > 1) {
					numberList[i] = str[0] + "." + str[1].substring(0, 2) + "%";
				} else {
					numberList[i] = str[0] + ".00" + "%";
				}
			}
		}
		bookCore.setNumberList(numberList);
	}

	/**
	 * 处理书的而平均分
	 */
	public void commmentGradeAvg(BookCore bookCore, Map<String, Object> paramMap) {
		// 查询本书的平均分
		Float ageGrade = bookCommentServiceImpl.getByMapAvg(paramMap);

		String ageGradeStr = null;
		if (ageGrade != null) {
			String str[] = ageGrade.toString().split("[.]");
			if (str.length > 1) {
				if (str[1].length() > 2) {
					ageGradeStr = str[0] + "." + str[1].substring(0, 2);
				} else {
					if (str[1].length() == 1) {
						ageGradeStr = str[0] + "." + str[1] + "0";
					} else {
						ageGradeStr = str[0] + "." + str[1];
					}

				}
			} else {
				ageGradeStr = str[0] + ".00";
			}
		} else {
			ageGradeStr = "0.00";
		}

		bookCore.setAgeGrade(ageGradeStr);
	}

	/**
	 * 整合评论下的信息
	 * 
	 * @param bookCommentList
	 * @param paramMap
	 */
	public void commentContent(BookCore bookCore, List<BookComment> bookCommentList, Map<String, Object> paramMap) {
		if (!bookCommentList.isEmpty()) {
			for (BookComment bookComment : bookCommentList) {
				// 查找回复
				paramMap.put("type", bookComment.getId());
				bookComment.setBookReplyCommentList(bookCommentServiceImpl.listByMap(paramMap));

				// 查点赞总数
				paramMap.put("commentId", bookComment.getId());
				Long bookAssistAccount = bookAssistServiceImpl.getByMapTotal(paramMap);
				if (StringUtil.isEmpty(bookAssistAccount)) {
					bookComment.setBookAssistAccount(0);
				} else {
					bookComment.setBookAssistAccount(Integer.parseInt(bookAssistAccount.toString()));
				}

				// 查询当前用户是否点过赞
				paramMap.put("userId", bookComment.getId());
				Long bookAssistNumber = bookAssistServiceImpl.getByMapTotal(paramMap);
				if (StringUtil.isEmpty(bookAssistNumber)) {
					bookComment.setTagsOrNot(false);
				} else {
					bookComment.setTagsOrNot(true);
				}
			}
		}
		bookCore.setBookCommentList(bookCommentList);
	}

	
	
}
